<?php

namespace app\services;

use app\traits\Page;
use plugin\admin\app\model\Activity;
use plugin\admin\app\model\Goods;
use plugin\admin\app\model\Like;
use plugin\admin\app\model\UserCollect;
use plugin\admin\app\model\Video;

class UserCollectService
{
    public static function videoList($userId, $page = 1, $length = 10)
    {
        $offset = ($page - 1) * $length;
        $query = UserCollect::where('user_id', $userId)->where('type', UserCollect::VIDEO)->orderByDesc('created_time');

        $totalCount = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->get()->toArray();

        $videoIds = array_column($list, 'third_party_id');
        $videos = Video::select(['id', 'thumb', 'likes', 'money'])->whereIn('id', $videoIds)->get()->toArray();

        foreach ($videos as $key=>$item) {
            $videos[$key]['thumb'] = full_url($item['thumb']);
        }

        return Page::wrap($totalCount, $page, $length, $videos);
    }

    public static function collect($type, $id, callable $callback = null)
    {
        if (! self::isCollect($type, $id)) {
            UserCollect::insert([
                'user_id' => get_user_id(),
                'thirdparty_id' => $id,
                'type' => $type,
                'created_time' => date('Y-m-d H:i:s'),
            ]);

            if (!is_null($callback)) {
                $callback($type, $id);
            }
        }
    }

    public static function list($page, $length, $type, $name = '')
    {
        $offset = ($page - 1) * $length;
        $query = UserCollect::where('user_id', get_user_id())->where('type', $type);

        if ($name) {
            $query = $query->where('name', 'like', "%{$name}%");
        }

        $count = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->orderByDesc('created_time')->get()->toArray();

        $thirdPartyIds = array_column($list, 'thirdparty_id');

        if ($type == UserCollect::ACTIVITY) {
            $list = Activity::select(['id', 'img_url', 'title', 'desc', 'created_time', 'collects', 'shares'])->whereIn('id', $thirdPartyIds)->get()->toArray();

            foreach ($list as $key=>$item) {
                $list[$key]['img_url'] = full_url($item['img_url']);
            }
        } else if ($type == UserCollect::GOODS) {
            $list = Goods::select(['id', 'thumb', 'name', 'price'])->whereIn('id', $thirdPartyIds)->get()->toArray();

            foreach ($list as $key=>$item) {
                $list[$key]['thumb'] = full_url($item['thumb']);
                $list[$key]['tags'] = GoodsService::getTags($item['id']);
            }
        }


        return Page::wrap($count, $page, $length, $list);

    }

    public static function count($type = null)
    {
        if (is_null($type)) {
            return UserCollect::where('user_id', get_user_id())->count();
        }
        return UserCollect::where('type', $type)->where('user_id', get_user_id())->count();
    }

    public static function uncollect($type, $id, callable $callback = null)
    {
        UserCollect::whereIn('thirdparty_id', (array)$id)->where('type', $type)->where('user_id', get_user_id())->delete();
        if (!is_null($callback)) {
            $callback($type, $id);
        }
    }

    public static function isCollect($type, $id)
    {
        return UserCollect::where('thirdparty_id', $id)->where('type', $type)->where('user_id', get_user_id())->count() > 0;
    }
}